#include <criterion/criterion.h>
#include <unistd.h>
#include <stdbool.h>
#include <pthread.h>
#include <criterion/theories.h>
#include "broadcast.h"

static int carNo = 0;

typedef struct
{
    char license[16];
    bool in;
} LicenseTest;

#define LIC_IN_LEN 100
#define LIC_MIX_LEN 10

static LicenseTest licMix[LIC_MIX_LEN] = {
    {"粤A 000001", false},
    {"粤A 662530", false},
    {"粤A 000001", true},
    {"粤A 662531", false},
    {"粤A 662531", false},
    {"粤A 662534", false},
    {"粤A 662543", false},
    {"粤A 662540", false},
    {"粤A 000001", false},
    {"粤A 000001", true},
};

static LicenseTest licIn[LIC_IN_LEN] = {
    {"粤A 662530", true},
    {"粤A 662531", true},
    {"粤A 662532", true},
    {"粤A 662533", true},
    {"粤A 662534", true},
    {"粤A 662535", true},
    {"粤A 662536", true},
    {"粤A 662537", true},
    {"粤A 662538", true},
    {"粤A 662539", true},
    {"粤A 662540", true},
    {"粤A 662541", true},
    {"粤A 662542", true},
    {"粤A 662543", true},
    {"粤A 662544", true},
    {"粤A 662545", true},
    {"粤A 662546", true},
    {"粤A 662547", true},
    {"粤A 662548", true},
    {"粤A 662549", true},
    {"粤A 662550", true},
    {"粤A 662551", true},
    {"粤A 662552", true},
    {"粤A 662553", true},
    {"粤A 662554", true},
    {"粤A 662555", true},
    {"粤A 662556", true},
    {"粤A 662557", true},
    {"粤A 662558", true},
    {"粤A 662559", true},
    {"粤A 662560", true},
    {"粤A 662561", true},
    {"粤A 662562", true},
    {"粤A 662563", true},
    {"粤A 662564", true},
    {"粤A 662565", true},
    {"粤A 662566", true},
    {"粤A 662567", true},
    {"粤A 662568", true},
    {"粤A 662569", true},
    {"粤A 662570", true},
    {"粤A 662571", true},
    {"粤A 662572", true},
    {"粤A 662573", true},
    {"粤A 662574", true},
    {"粤A 662575", true},
    {"粤A 662576", true},
    {"粤A 662577", true},
    {"粤A 662578", true},
    {"粤A 662579", true},
    {"粤A 662580", true},
    {"粤A 662581", true},
    {"粤A 662582", true},
    {"粤A 662583", true},
    {"粤A 662584", true},
    {"粤A 662585", true},
    {"粤A 662586", true},
    {"粤A 662587", true},
    {"粤A 662588", true},
    {"粤A 662589", true},
    {"粤A 662590", true},
    {"粤A 662591", true},
    {"粤A 662592", true},
    {"粤A 662593", true},
    {"粤A 662594", true},
    {"粤A 662595", true},
    {"粤A 662596", true},
    {"粤A 662597", true},
    {"粤A 662598", true},
    {"粤A 662599", true},
    {"粤A 662600", true},
    {"粤A 662601", true},
    {"粤A 662602", true},
    {"粤A 662603", true},
    {"粤A 662604", true},
    {"粤A 662605", true},
    {"粤A 662606", true},
    {"粤A 662607", true},
    {"粤A 662608", true},
    {"粤A 662609", true},
    {"粤A 662610", true},
    {"粤A 662611", true},
    {"粤A 662612", true},
    {"粤A 662613", true},
    {"粤A 662614", true},
    {"粤A 662615", true},
    {"粤A 662616", true},
    {"粤A 662617", true},
    {"粤A 662618", true},
    {"粤A 662619", true},
    {"粤A 662620", true},
    {"粤A 662621", true},
    {"粤A 662622", true},
    {"粤A 662623", true},
    {"粤A 662624", true},
    {"粤A 662625", true},
    {"粤A 662626", true},
    {"粤A 662627", true},
    {"粤A 662628", true},
    {"粤A 662629", true},
};

Test(broaadcastTest, send_and_recv)
{
    cr_skip_test();
    pthread_t recvThread;

    initBroadcast();
    pthread_create(&recvThread, NULL, (void *)broadcastServer, NULL);
    sleep(2);
    LOG_NOTICE("广播测试服务器初始化完成");

    log_init("log_all", "./conf/zlog.conf");
    setBroadcastType(true);

    int sock;
    for (size_t i = 0; i < LIC_IN_LEN; i++)
    {
        getUdpClientSock(&sock);
        LicenseTest tmp = licIn[i];
        setBroadcastType(tmp.in == true ? true : false);
        sendBroadcast(sock, tmp.license);
    }
    sleep(1);
    cr_assert_eq(getParkCarNum(), 50);

    for (size_t i = 0; i < LIC_MIX_LEN; i++)
    {
        getUdpClientSock(&sock);
        LicenseTest tmp = licMix[i];
        setBroadcastType(tmp.in == true ? true : false);
        sendBroadcast(sock, tmp.license);
    }
    sleep(1);
    LOG_INFO("%d", getParkCarNum());
    cr_assert_eq(getParkCarNum(), 46);
}
